package www.openteam.com.model.business;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.InsertStatus;
import www.openteam.com.bean.QueryCustom;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by hyd on 2016/10/15.
 */

public class EditCustomInfoModel extends BaseModel {
    private boolean isQuery = false;
    private boolean isQueryM = false;
    private boolean isQueryC = false;

    public boolean queryCocustomInfos(String code, String name, String Businessman, int UserID,int PageSize,
                                     final QueryCustomInfoOnListener editCustomInfoOnListener) {
        httpApiService.queryCocustomInfos(code, name, Businessman,UserID,PageSize)
                .compose(new CommonTransformer<QueryCustom>())
                .subscribe(new CommonSubscriber<QueryCustom>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(QueryCustom queryCustom) {
//                        queryCustom.getData().get(0).setName("***");
//                        queryCustom.getData().get(0).setBusinessMan("系统管理员");
//                        queryCustom.getData().get(0).setConnecter("***");
                        isQuery = true;
                        editCustomInfoOnListener.queryCISuccess(queryCustom);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQuery = false;
                        editCustomInfoOnListener.queryCIFail(e);
                    }
                });
        return isQuery;
    }

    public boolean queryMoneyTypeInfo(int pageSize, final QueryMTIOnListener editCustomInfoOnListener) {
        httpApiService.queryMoneyTypeInfo(pageSize)
                .compose(new CommonTransformer<CustomType>())
                .subscribe(new CommonSubscriber<CustomType>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(CustomType customType) {
                        isQueryM = true;

                        editCustomInfoOnListener.queryMTISuccess(customType);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQueryM = false;
                        editCustomInfoOnListener.queryMTIFail(e);
                    }
                });
        return isQueryM;
    }


    public boolean queryCopayTypeInfo(int pageSize, final QueryCPTIOnListener editCustomInfoOnListener) {
        httpApiService.queryCopayTypeInfo(pageSize)
                .compose(new CommonTransformer<CustomType>())
                .subscribe(new CommonSubscriber<CustomType>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(CustomType customType) {
                        isQueryC = true;
                        editCustomInfoOnListener.queryCPTISuccess(customType);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQueryC = false;
                        editCustomInfoOnListener.queryCPTIFail(e);
                    }
                });
        return isQueryC;
    }

    public boolean queryCocustomTypeInfo(int pageSize, final QueryCTIOnListener editCustomInfoOnListener) {


        httpApiService.queryCocustomTypeInfo(pageSize)
                .compose(new CommonTransformer<CustomType>())
                .subscribe(new CommonSubscriber<CustomType>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(CustomType customType) {
                        isQueryC = true;
                        editCustomInfoOnListener.queryCTISuccess(customType);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQueryC = false;
                        editCustomInfoOnListener.queryCTIFail(e);
                    }
                });
        return isQueryC;
    }

    public boolean deleteCustomInfo(int id, final DelectCustomInfoOnListener editCustomInfoOnListener) {

        httpApiService.deleteCustomInfo(id)
                .compose(new CommonTransformer<InsertStatus>())
                .subscribe(new CommonSubscriber<InsertStatus>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(InsertStatus insertStatus) {
                        isQueryC = true;
                        editCustomInfoOnListener.deleteCISuccess(insertStatus);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQueryC = false;
                        editCustomInfoOnListener.deleteCIFail(e);
                    }
                })
        ;
        return isQueryC;

    }

    public interface QueryCustomInfoOnListener {
        void queryCISuccess(QueryCustom queryCustom);

        void queryCIFail(Throwable throwable);
    }

    public interface DelectCustomInfoOnListener {
        void deleteCISuccess(InsertStatus insertStatus);

        void deleteCIFail(Throwable throwable);
    }

    public  interface QueryCTIOnListener{
        void queryCTISuccess(CustomType customType);

        void queryCTIFail(Throwable throwable);
    }

    public interface  QueryCPTIOnListener{

        void queryCPTISuccess(CustomType customType);

        void queryCPTIFail(Throwable throwable);

    }

    public  interface  QueryMTIOnListener{
        void queryMTISuccess(CustomType customType);

        void queryMTIFail(Throwable throwable);
    }

}
